package liangtWorkSpace.Traders;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;

/**
 * @ClassName TraderMain
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/27 22:11
 */
public class TraderMain {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        /**
         * 找出所有2011年的交易并排序
         */
        List<Transaction> tr2011 = transactions.stream()
                .filter(transaction -> transaction.getValue() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());


        /**
         * 获取所有的城市
         */
        List<String> cities = transactions.stream()
                .map(d -> d.getTrader().getCity())
                .distinct()
                .collect(Collectors.toList());

        /**
         * 通过set替代Distinct
         */
        Set<String> citiesSet = transactions.stream().
                map(transaction -> transaction.getTrader().getCity())
                .collect(Collectors.toSet());

        /**
         * 获取剑桥地区的人员名称并排序
         */
        List<String> nameList = transactions.stream()
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
                .map(transaction -> transaction.getTrader().getName())
                .sorted()
                .collect(Collectors.toList());

        /**
         * 将所有名字排序并连接成一个串
         */
        String traderStr = transactions.stream()
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted()
                .collect(joining());
        /**
         * 是否存在在米兰工作的人员
         */
        boolean milanBased = transactions.stream()
                .anyMatch(transaction -> transaction.
                        getTrader().getCity()
                        .equals("Milan"));

        /**
         * 输出所有人的值
         */
        transactions.stream()
                .map(transaction -> transaction.getValue())
                .forEach(System.out::println);

        /**
         * 获取值最大的信息
         */
        Optional<Integer> mxa = transactions.stream()
                .map(transaction -> transaction.getValue())
                .reduce(Integer::max);

        /**
         * 获取值最小的
         */
        Optional<Transaction> min = transactions.stream()
                .reduce((a, b) -> a.getValue() < b.getValue() ? a : b);


        /**
         * 求和
         */
        int sum = transactions.stream()
                .mapToInt(Transaction::getValue)
                .sum();

        /**
         * boxed原流转化为一般流
         */
        Stream<Integer> stream = transactions.stream()
                .mapToInt(Transaction::getValue).boxed();

        /**
         * 获取一个100以内偶数流
         * 如果使用range（1，100）只会获取到49个
         */
        IntStream intStream = IntStream.rangeClosed(1, 100)
                .filter(n -> n % 2 == 0);

        /**
         * 统计偶数个数
         */
        System.out.println(intStream.count());

        /**
         * 勾股数
         */
        Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100).boxed()
                        .flatMap(a ->
                                IntStream.rangeClosed(a, 100)
                                        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                                        .mapToObj(b ->
                                                new int[]{a, b, (int) Math.sqrt(a * a + b * b)})
                        );
        pythagoreanTriples.limit(5)
                .forEach(t ->
                        System.out.println(t[0] + ", " + t[1] + ", " + t[2]));


//        long uniqueWords = 0;
//
//        try(Stream<String> lines = Files.lines(Paths.get(""), Charset.defaultCharset())){
//            uniqueWords = lines.flatMap(lines->Arrays.stream(lines.split(" ")))
//                    .distinct()
//                    .count();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
